const http = uni.$u.http

const install = (Vue, vm) => {

	// post 请求
	let post = async (url = '', params = {}, auth) => {
		let data = {
			...params,
			time: new Date().getTime(),		
		}
		return http.post(url, data, {header: {
			"Authorization": uni.getStorageSync('token')			
		}})
	}
	// get 请求
	let get = (url = '', params = {}, auth) => {
		let data = {
			...params,
			time: new Date().getTime(),
		}
		if (!uni.getStorageSync('token') || !uni.getStorageSync('username')) {
			if (auth) {
				uni.reLaunch({
					url: '/pages/login/login'
				})
				return;
			}
		}
		return http.get(url, {
			params: data,			
			header: {
				"Authorization": uni.getStorageSync('token')				
			}
		})
	}
	let getopenid = (params) => { // 获取学校列表
		return new Promise((resolve, reject) => {
			post('/api/nft-school/auth/getopenid', params, false).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getSchoolList = (params) => { // 获取学校列表
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/getSchoolList', params, false).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getSchoolAllList = (params) => { // 获取全部学校列表
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/getSchoolAllList', params, false).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getSchoolDetail = (params) => { // 获取学校详情
		return new Promise((resolve, reject) => {
			get('/api/nft-school/userApp/getSchoolInfo', params, false).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getNoticeList = (params) => { // 获取活动公告列表
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/getNoticeList', params, false).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getNotice = (params) => { // 获取公告详情
		return new Promise((resolve, reject) => {
			get('/api/nft-school/userApp/getNoticeInfo', params, false).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getCode = (params) => { // 获取验证码
		return new Promise((resolve, reject) => {
			get('/api/nft-school/auth/getPhoneCode', params, false).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let codeLogin = (params) => { // 验证码登录
		return new Promise((resolve, reject) => {
			post('/api/nft-school/auth/loginByCode', params, false).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getUserInfo = (params) => { // 获取用户信息
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/getUserAppInfoByPhone', params, true).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getUserInfoByUserId = (params) => { // 获取用户信息
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/getUserAppInfoByUserId', params, true).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let modifyUserInfo = (params) => { // 修改用户信息
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/updateUserAppInfo', params, true, true).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getUserSchoolListByPhone = (params) => { // 我的学校
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/getUserSchoolListByPhone', params, true).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getUserClassesListByUserId = (params) => { // 我的班级
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/getUserClassesListByUserId', params, true).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getConsume = (params) => { // 获取我的耗课列表
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/getConsume', params, true).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getXsConsume = (params) => { // 获取我的耗课列表
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/getXsConsume', params, true).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let realName = (params) => { // 实名认证
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/realNameUser', params, true).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let realConsume = (params) => { // 耗课信息
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/realConsume', params, true).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}
	let getXsList = (params) => { // 获取学生列表信息
		return new Promise((resolve, reject) => {
			post('/api/nft-school/userApp/getXsList', params, false).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			});
		})
	}	
	// 将各个定义的接口名称，统一放进对象挂载到vm.$u.api(因为vm就是this，也即this.$u.api)下
	vm.$u.api = {
		getopenid,
		getSchoolList,
		getSchoolAllList,
		getSchoolDetail,
		getNoticeList,
		getNotice,
		getCode,
		codeLogin,
		getUserInfo,
		getUserInfoByUserId,
		modifyUserInfo,
		getUserSchoolListByPhone,
		getUserClassesListByUserId,
		getConsume,
		getXsConsume,
		realName,
		realConsume,
		getXsList		
	}
}
export default install
